package logger

import (
	"context"
	"fmt"
	"path/filepath"
	"time"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/glog"
	"github.com/toolkits/file"
)

var (
	_logger *glog.Logger
	err     error
	ctx     context.Context
)

func init() {
	ctx, _ = context.WithTimeout(context.Background(), 10*time.Second)
}

func InitLogger(name string) {
	logger := glog.New()
	logger.SetConfigWithMap(g.Map{
		"path":     filepath.Join(file.SelfDir(), "logs"),
		"level":    "all",
		"stdout":   true,
		"StStatus": 0,
		"file":     fmt.Sprintf("%s.log", name),
	})

	_logger = logger
}

func Info(msg string) {
	_logger.Info(ctx, msg)
}

func Infof(format string, args ...interface{}) {
	_logger.Infof(ctx, format, args...)
}

func Error(msg string) {
	_logger.Error(ctx, msg)
}

func Errorf(format string, args ...interface{}) {
	_logger.Errorf(ctx, format, args...)
}

func Warning(msg string) {
	_logger.Warning(ctx, msg)
}

func Warningf(format string, args ...interface{}) {
	_logger.Warningf(ctx, format, args...)
}

func Critical(msg string) {
	_logger.Critical(ctx, msg)
}

func Criticalf(format string, args ...interface{}) {
	_logger.Criticalf(ctx, format, args...)
}
